<script>
import authService from './services/auth.js'

export default {
  onLaunch: function () {
    console.log('App Launch')
    this.checkInitialRoute()
  },
  onShow: function () {
    console.log('App Show')
  },
  onHide: function () {
    console.log('App Hide')
  },
  methods: {
    checkInitialRoute() {
      // 检查当前页面路径
      const pages = getCurrentPages()
      const currentPage = pages[pages.length - 1]
      const currentRoute = currentPage ? currentPage.route : ''
      
      console.log('当前页面路径:', currentRoute)
      
      // 需要登录才能访问的页面
      const protectedRoutes = [
        'pages/home/home',
        'pages/profile/profile', 
        'pages/address/address',
        'pages/orders/orders',
        'pages/order/order',
        'pages/group-buying/group-buying'
      ]
      
      // 检查登录状态
      const loginStatus = authService.checkLoginStatus()
      console.log('登录状态:', loginStatus)
      
      // 如果访问受保护的页面但未登录，跳转到登录页
      if (protectedRoutes.includes(currentRoute) && !loginStatus.isLoggedIn) {
        console.log('未登录，跳转到登录页')
        uni.reLaunch({
          url: '/pages/login/login'
        })
      }
      // 如果已登录但在登录页，跳转到首页
      else if (currentRoute === 'pages/login/login' && loginStatus.isLoggedIn) {
        console.log('已登录，跳转到首页')
        uni.reLaunch({
          url: '/pages/home/home'
        })
      }
    }
  }
}
</script>

<style>
/*每个页面公共css */
</style>
